#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	// Testing redirects and https.
	camWidth = 640;  // try to grab at this size.
	camHeight = 480;
	lastSaveTime = 0;
	
	ofSetFrameRate(1200);
	
	//we can now get back a list of devices.
	vector<ofVideoDevice> devices = vidGrabber.listDevices();
	camNum = devices.size();
	for (int i = 0; i < devices.size(); i++) {
		if (devices[i].bAvailable) {
			ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
			camera[i] = new myCamera(camWidth, camHeight, i);
		}
		else {
			ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
		}
	}
	lastSaveSystime = time(NULL);
	lastSendHeardTime = time(NULL);
	ofSetVerticalSync(true);
}

//--------------------------------------------------------------
void ofApp::update(){
	ofBackground(100, 100, 100);
	for (int i = 0; i < camNum; i++)
	{
		camera[i]->updateFlame();		

	}
	systime = time(NULL);
	struct tm *nowTime;
	nowTime= localtime(&systime);
	long diff = systime - lastSaveSystime;
	if (systime%node.heardFlame == 0 && systime > lastSendHeardTime)
	{
		lastSendHeardTime = systime;
		bool ifSendHeardSuccess=node.sendHeard();
		if (ifSendHeardSuccess)
		{
			ofLogNotice() << "send heard"<< nowTime;
		}
	}
	if ((systime%node.imageFlame ==0&&systime>lastSaveSystime)||(diff>node.imageFlame))
	{
	
		lastSaveSystime = systime;
		for (int i = 0; i < camNum; i++)
		{
			char fileNametemp[80];
			
			strftime(fileNametemp, 80, "Date%Y%m%d%H%M%S", nowTime);
			stringstream stream;
			stream<<"D:\\mycameraImage\\" <<"cam"<< i<< fileNametemp<<".jpg";
			string fileName = stream.str();
			ofLogNotice()<<fileName << "get file";
			camera[i]->saveFlame(fileName);

			bool ifSendImageSuccess=node.sendImage(fileName);
			if (ifSendImageSuccess)
			{

			}
		

		}
	}
	

}

//--------------------------------------------------------------
void ofApp::draw(){
	ofSetHexColor(0xffffff);
	int widthTmp = 20;
	int heightTmp = 20;
	for (int i = 0; i < camNum; i++)
	{
		camera[i]->vidGrabber.draw(widthTmp, heightTmp);
		widthTmp += 640;
		heightTmp += 480;
	}
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
	if (key == 's' || key == 'S') {
		vidGrabber.videoSettings();
	}
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

